//
//  ClassifyView.m
//  gpos_app
//
//  Created by 崔航飞 on 2018/8/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ClassifyView.h"
#define PROVINCE_COMPONENT  0
#define CITY_COMPONENT      1
#define DISTRICT_COMPONENT  2
#define KScreenWidth  [UIScreen mainScreen].bounds.size.width
#define KScreenHeight  [UIScreen mainScreen].bounds.size.height

@implementation ClassifyView

- (instancetype)init
{
    if (self = [super init]) {
        [self initCityPickerView];
    }
    return self;
}
- (void)initCityPickerView{
    //原来的赋值
//    [self getCityData];
    self.frame=[UIScreen mainScreen].bounds;
    self.backgroundColor =  [[UIColor blackColor]colorWithAlphaComponent:0.4];
    //一个选择器
//    _cityPicker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, KScreenHeight-216, KScreenWidth, 216)];
_cityPicker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, 216)];
    
    _cityPicker.delegate=self;
    _cityPicker.dataSource=self;
    _cityPicker.backgroundColor=[UIColor whiteColor];
    
    
    //一个工具栏
//    UIView *viewTool=[[UIView alloc] initWithFrame:CGRectMake(0, KScreenHeight-216-44, KScreenWidth, 44)];
    UIView *viewTool=[[UIView alloc] initWithFrame:CGRectMake(0, KScreenHeight-44, KScreenWidth, 44)];
    
    
    UIColor *color=[UIColor colorWithRed:230/256.0 green:230/256.0 blue:230/256.0 alpha:1];
    viewTool.backgroundColor=color;
    [self addSubview:viewTool];
    //一个取消按钮
    UIButton *btnCancel=[[UIButton alloc] initWithFrame:CGRectMake(5, 5, 40, 30)];
    [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [btnCancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btnCancel setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [btnCancel addTarget:self action:@selector(btnCancelAction) forControlEvents:UIControlEventTouchUpInside];
    [viewTool addSubview:btnCancel];
    //一个确定按钮
    UIButton *btnSure=[[UIButton alloc] initWithFrame:CGRectMake(KScreenWidth-45, 5, 40, 30)];
    [btnSure setTitle:@"确定" forState:UIControlStateNormal];
    [btnSure setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btnSure setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [btnSure addTarget:self action:@selector(btnSureAction) forControlEvents:UIControlEventTouchUpInside];
    [viewTool addSubview:btnSure];
    //一个显示城市的lab
    _btnSelectShow=[[UILabel alloc] initWithFrame:CGRectMake(50, 5, KScreenWidth-100, 30)];
    _btnSelectShow.text=@"娱乐";
    _btnSelectShow.textAlignment=1;
//    [viewTool addSubview:_btnSelectShow];
//    [self animationWithView:_cityPicker duration:0.5];
    
    [UIView animateWithDuration:0.6 animations:^{
        
        [self.cityPicker setFrame:CGRectMake(0, KScreenHeight-216, KScreenWidth, 216)];
        
        [viewTool setFrame:CGRectMake(0, KScreenHeight-216-44, KScreenWidth, 44)];
    
    } completion:nil];
    [self addSubview:_cityPicker];
    [self showSkyerCityPicker];
}
/*
- (void)animationWithView:(UIView *)view duration:(CFTimeInterval)duration{
    
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = duration;
    animation.removedOnCompletion = NO;

    animation.fillMode = kCAFillModeForwards;

    NSMutableArray *values = [NSMutableArray array];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];


    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    
    
    [view.layer addAnimation:animation forKey:nil];
    
}
 */
#pragma mark - 数据初始化
- (void)getCityData:(NSMutableArray *)arr{

//    _arrProvince=@[@"娱乐",@"饮食",@"酒店"];
    _arrProvince =arr;

}

#pragma mark- 选择器的数据源代理

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
        return [_arrProvince count];
    
}


#pragma mark- 选择器的代理方法

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   _btnSelectShow.text = [_arrProvince objectAtIndex:row];
}


- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return KScreenWidth;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    
        UILabel *myView;
   
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, KScreenWidth, 50)];
        myView.textAlignment = NSTextAlignmentCenter;
        myView.text = [_arrProvince objectAtIndex:row] ;
        myView.font = [UIFont systemFontOfSize:18];
        myView.backgroundColor = [UIColor clearColor];

    return myView;
}
#pragma mark -block获取选择的城市词典
- (void)cityPickerSetSelectCity{
   
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(backName:)]) {
        [self.delegate backName:_btnSelectShow.text];
    }
}

- (void)btnCancelAction{
    [self disMissCityPicker];
}
- (void)btnSureAction{
    [self cityPickerSetSelectCity];
    [self disMissCityPicker];
}
- (void)showSkyerCityPicker{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.center = [UIApplication sharedApplication].keyWindow.center;
}
-(void)disMissCityPicker{
    [self removeFromSuperview];
}

@end
